package customerEnter.Frame.oss;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

import customerEnter.Frame.Utils.DateUtils;
import customerEnter.tool.WhetherToEmpty;

/**
 * oss服務
 * 
 * 类名称：OssService 类描述： 创建人：dy 创建时间：2016-6-2 上午10:50:25
 * 
 */
public class OssService {
	private static OSSClient	ossClient;

	/**
	 * 打開oss服務
	 */
	public static void OpenClient() {
		if (WhetherToEmpty.ObjectWhetherToEmpty(ossClient)) {
			ossClient = OSS.getOssClient();
		}
	}

	/**
	 * 上傳文件到系統
	 */
	public static String UploadSystemImg(String imgUrl) throws Exception {

		return OSS.getOss().uploadFile(ossClient, imgUrl,
				"systemImg\\" + DateUtils.getDay() + "\\" + DateUtils.getMoth() + "\\" + DateUtils.getDay());
	}

	/**
	 * 上傳文件到系統
	 */
	public static String UploadObject(String imgUrl, String objcet) throws Exception {

		return OSS.getOss().uploadFile(ossClient, imgUrl,
				objcet + "\\" + DateUtils.getDay() + "\\" + DateUtils.getMoth() + "\\" + DateUtils.getDay());
	}

	/**
	 * 上傳文件到系統
	 */
	public static String UploadObject(File imgFile, String objcet) throws Exception {
		return OSS.getOss().uploadFile(ossClient, imgFile,
				objcet + "\\" + DateUtils.getDay() + "\\" + DateUtils.getMoth() + "\\" + DateUtils.getDay());
	}

	/**
	 * 獲取所有的object
	 */
	@SuppressWarnings("deprecation")
	public static List<String> getAllObject(Integer size, String ObjectName) {
		if (WhetherToEmpty.ObjectWhetherToEmpty(size)) {
			size = 0;
		}
		final int maxKeys = size;
		// 列举Object
		ListObjectsRequest objectsRequest = new ListObjectsRequest(OSSUseTransmission.getAucketName()).withMarker(
				ObjectName).withMaxKeys(maxKeys);
		ObjectListing objectListing = ossClient.listObjects(objectsRequest);
		List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
		System.out.println("總共有Object:" + objectListing.getObjectSummaries().size());
		List<String> list = new ArrayList<String>();
		for (OSSObjectSummary ossobj : sums) {
			String key = ossobj.getKey();
			list.add("http://mbuy.oss-cn-hongkong.aliyuncs.com/" + key + "#"
					+ ossobj.getLastModified().toLocaleString());
		}
		return list;
	}

	/**
	 * 分頁查詢
	 */
	public static List<String> getPageSizeObject(Integer size, String ObjectName) {
		if (WhetherToEmpty.ObjectWhetherToEmpty(size)) {
			size = 0;
		}
		final int maxKeys = size;
		ObjectListing objectListing = null;
		String nextMarker = null;
		List<String> list = new ArrayList<String>();
		do {
			objectListing = ossClient.listObjects(new ListObjectsRequest(OSSUseTransmission.getAucketName())
					.withMaxKeys(maxKeys).withMarker(nextMarker).withMarker(ObjectName));
			List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
			for (OSSObjectSummary ossobj : sums) {
				String key = ossobj.getKey();
				list.add("http://mbuy.oss-cn-hongkong.aliyuncs.com/" + key);
			}
			nextMarker = objectListing.getNextMarker();
		} while (objectListing.isTruncated());
		return list;
	}

	public static void getObjects() {
		System.out.println(" ");

	}

	/**
	 * 刪除
	 */
	public static boolean deleObject(String objectName) {
		Boolean boolean1 = false;
		try {
			OSS.deleteObject(ossClient, OSSUseTransmission.getAucketName(), objectName);
			boolean1 = true;
		} catch (Exception e) {
			boolean1 = false;
		}
		return boolean1;
	}

	/**
	 * 創建一個object
	 */
	public static boolean createObject(String objectName) {

		Boolean boolean1 = false;
		try {
			OSS.createObject(ossClient, objectName);
			boolean1 = true;
		} catch (Exception e) {
			boolean1 = false;
		}
		return boolean1;
	}

	/**
	 * 關閉oss服務
	 * 
	 */
	public static void CloasClient() {
		ossClient.shutdown();
	}

	public static void main(String[] args) {
		OssService.OpenClient();

		OssService.CloasClient();
	}

}
